package proto

import (
	"errors"
	"server/public"
)

/*
*  -*- coding:utf8 -*-
*
*	通信协议相关，通信格式指定等
*
*  @Author  : chengms
*  @WebSite : chengms.com
*  @Email   : chengms2012@163.com
*  @TIME    : 2020/8/16 下午6:11
*  @File    : command.go
 */

var (
	InvalidMessageErr = errors.New("invalid message")
	UnsupportedCmdErr = errors.New("unsupported cmd")
	CloseTcpErr       = errors.New("use of closed network connection")
)

const ClosedConnectionMsg = "use of closed network connection"

const (
	ProtocolName    = "MENG"
	ProtocolVersion = "v1.0"
	ProtocolSep     = "#" // 分隔字符
)

const (
	// 系统操作
	CmdHeartbeat = 0x0100 // 心跳包
	CmdStatus    = 0x0102 // 获取状态

	// 用户操作相关
	CmdLogin     = 0x0200 // 登录
	CmdLogOut    = 0x0201 // 离线
	CmdRegister  = 0x0202 // 注册
	CmdAddFriend = 0x0203 // 添加好友
	CmdAddGroup  = 0x0204 // 添加群

	// 消息发送
	CmdSend      = 0x0300 // 发送聊天消息
	CmdBroadCase = 0x0301 // 广播
	CmdGroup     = 0x0302 // 群聊
	CmdLeave     = 0x0303 // 离开群聊
)

type BasrCmd struct {
	Protocol string `json:"Protocol"`
	Vserion  string `json:"Version"`
}

type CommandBuf struct {
	BasrCmd `json:"BasrCmd"`
	CmdType int      `json:"CmdType"`
	Data    []string `json:"Data"`
}

func NewCommandBuf(cmdType int) *CommandBuf {
	return &CommandBuf{
		BasrCmd: BasrCmd{
			Protocol: ProtocolName,
			Vserion:  ProtocolVersion,
		},
		CmdType: cmdType,
	}
}

type SendCommand struct {
	CommandBuf
}

func (s *SendCommand) SetSendCmd(msg interface{}) error {
	data, _ := public.Jsonit.MarshalToString(msg)
	s.Data = append(s.Data, data)

	return nil
}

func NewSendCommand(cmdType int) *SendCommand {
	return &SendCommand{
		CommandBuf: *NewCommandBuf(cmdType),
	}
}
